package com.heima.search.listener;

import com.alibaba.fastjson.JSON;
import com.heima.common.constans.ArticleConstants;
import com.heima.model.search.SearchArticleVo;
import com.heima.search.service.SearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

/**
 * 同步数据到es的监听类
 */
@Component
public class SyncArticleListener {

    @Autowired
    private SearchService searchService;

    @KafkaListener(topics = ArticleConstants.ARTICLE_ES_SYNC_TOPIC)
    public void getMessage(String message){
        //1。类型转换
        SearchArticleVo searchArticleVo = JSON.parseObject(message, SearchArticleVo.class);
        //2.写入数据到ES中
        searchService.addArticleToEs(searchArticleVo);
    }
}
